package com.cmlite.adapter;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.cmlite.R;
import com.cmlite.model.EventModel;

public class EventAdapter extends ArrayAdapter<EventModel> implements
		SectionIndexer {

	private LayoutInflater inflater;
	private List<EventModel> mArr;
	private int layout;
	private Context mContext;
	private Resources mRes;
	private OnClickListListener listener;

	public EventAdapter(Context context, int textViewResourceId,
			List<EventModel> objects, OnClickListListener listener) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub

		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.layout = textViewResourceId;
		this.mArr = objects;
		this.mContext = context;
		this.mRes = context.getResources();
		this.listener = listener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArr.size();
	}

	@Override
	public EventModel getItem(int position) {
		// TODO Auto-generated method stub
		return mArr.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		final ViewHolder mHolder;

		if (view == null) {
			view = inflater.inflate(layout, null);
			mHolder = new ViewHolder();
			mHolder.tv_name = (TextView) view
					.findViewById(R.id.item_event_tv_name);
			mHolder.tv_type = (TextView) view
					.findViewById(R.id.item_event_tv_type);
			mHolder.layout_data = (LinearLayout) view
					.findViewById(R.id.item_event_layout_item);
			view.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) view.getTag();
		}

		if (mArr.size() > 0) {
			final EventModel mEvent = mArr.get(position);
			if (!mEvent.isGroup()) {
				mHolder.changeLayout(1);
				mHolder.tv_name.setText(mEvent.getEventName());
				if (mEvent.isColor())
					mHolder.layout_data
							.setBackgroundResource(R.xml.item_selector_listview_01);
				else
					mHolder.layout_data
							.setBackgroundResource(R.xml.item_selector_listview_02);

				mHolder.layout_data.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (!mEvent.isGroup()) {
							for (EventModel e : mArr) {
								if (e.isSelected()) {
									e.setSelected(false);
								}
							}
							mEvent.setSelected(true);
							listener.onClick(mEvent);
						}
					}
				});
				if (mEvent.isSelected()) {
					mHolder.layout_data.setSelected(true);
				} else {
					mHolder.layout_data.setSelected(false);
				}
			} else {
				mHolder.tv_type.setText(mEvent.getEventName());
				mHolder.changeLayout(0);
			}

		}

		return view;
	}

	class ViewHolder {
		TextView tv_name, tv_type;
		LinearLayout layout_data;

		public void changeLayout(int i) {
			if (i == 0) { // type
				layout_data.setVisibility(View.GONE);
				tv_type.setVisibility(View.VISIBLE);
			} else { // data
				layout_data.setVisibility(View.VISIBLE);
				tv_type.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		try {
			if (mArr != null || mArr.size() > 0) {
				if (section == 35) { // 35
					return 0;
				}
				for (int i = 0; i < mArr.size(); i++) {
					if (mArr.get(i).getEventName() != null
							&& mArr.get(i).getEventName().length() > 0) {
						String l = mArr.get(i).getEventName();
						char firstChar = l.toUpperCase().charAt(0);
						if (firstChar == section) {
							return i;
						}
					}

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}

	public static interface OnClickListListener {
		public abstract void onClick(EventModel data);
	}
}
